Write-Host "欢迎来到Windows版bash自动安装系统" -ForegroundColor Blue

try {
    Write-Output "正在从清华镜像源安装bash"
    if (Test-Path "C:\Program Files\Git\bin\bash.exe") {
        Write-Output "bash已安装"
    } else {
        Write-Output "正在下载bash安装包"
        $url = "https://mirrors.tuna.tsinghua.edu.cn/git/git-for-windows/releases/download/v2.33.0.windows.2/Git-2.33.0-64-bit.exe"
        $output = "C:\Users\Administrator\Downloads\Git-2.33.0-64-bit.exe"
        
        # 下载bash安装包
        try {
            $start_time = Get-Date
            (New-Object Net.WebClient).DownloadFile($url, $output)
            Write-Output "下载完成，用时 $($((Get-Date).Subtract($start_time).Seconds)) 秒"
        } catch {
            Write-Host "下载失败: $_" -ForegroundColor Red
            exit
        }

        Write-Output "正在安装bash"
        # 安装bash
        try {
            Start-Process -FilePath $output -ArgumentList "/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS=""core,ext,ext\shellhere,ext\guihere,assoc,assoc_sh"" /LOG=""C:\Users\Administrator\Downloads\Git-2.33.0-64-bit.log""" -Wait
            Write-Output "安装完成"
        } catch {
            Write-Host "安装失败: $_" -ForegroundColor Red
            exit
        }

        Write-Output "正在配置环境变量"
        try {
            $env:Path += ";C:\Program Files\Git\bin"
            [Environment]::SetEnvironmentVariable("Path", $env:Path, "User")
            Write-Output "环境变量配置完成"
        } catch {
            Write-Host "环境变量配置失败: $_" -ForegroundColor Red
            exit
        }
    }
} catch {
    Write-Host "安装过程中发生错误: $_" -ForegroundColor Red
}

Write-Host "© GlueStudio 2024" -ForegroundColor Orange
Write-Output "正在启动bash..."
Start-Process -FilePath "C:\Program Files\Git\bin\bash.exe" -ArgumentList "--login"
